{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# State preparation\n",
    "\n",
    "State preparation  is a very important part of many quantum algorithms. How a state is prepared can have a huge effect on the circuit characteristics. Here we will prepare the same state twice with different approximations to see how that can affect the generated circuit."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T14:26:44.339036Z",
     "iopub.status.busy": "2024-05-07T14:26:44.338782Z",
     "iopub.status.idle": "2024-05-07T14:26:47.488470Z",
     "shell.execute_reply": "2024-05-07T14:26:47.487799Z"
    }
   },
   "outputs": [],
   "source": [
    "from classiq import *\n",
    "\n",
    "amplitudes = [\n",
    "    0.2450344728207077,\n",
    "    -0.1700813505441722,\n",
    "    0.16769736623372838,\n",
    "    -0.0239052232765222,\n",
    "    0.34393605643711794,\n",
    "    -0.2966929669473228,\n",
    "    -0.2839167245810649,\n",
    "    0.3305828537849893,\n",
    "    -0.32376029368963405,\n",
    "    0.25704647614347453,\n",
    "    0.1531631416826692,\n",
    "    -0.07516264976664182,\n",
    "    -0.314744872342388,\n",
    "    -0.24992215398592088,\n",
    "    0.3526620654512262,\n",
    "    -0.015039183255468514,\n",
    "]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Create a circuit to prepare the amplitudes\n",
    "\n",
    "Use the `prepare_amplitudes()` function to encode the amplitudes above into a quantum circuit. Use the defined `bound` in your `prepare_amplitudes()` function. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T14:26:47.495799Z",
     "iopub.status.busy": "2024-05-07T14:26:47.495065Z",
     "iopub.status.idle": "2024-05-07T14:26:49.970730Z",
     "shell.execute_reply": "2024-05-07T14:26:49.969877Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Opening: https://platform.classiq.io/circuit/0cf6b636-9189-4280-9e13-e25517c8e1eb?version=0.41.0.dev39%2B79c8fd0855\n"
     ]
    }
   ],
   "source": [
    "bound = 0.3\n",
    "\n",
    "\n",
    "@qfunc\n",
    "def main(\n",
    "    # target: Output[QArray]\n",
    "):\n",
    "    # Your code here\n",
    "    pass\n",
    "\n",
    "\n",
    "qprog = synthesize(create_model(main))\n",
    "show(qprog)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Change the bound for `0.001` and look at the effect.\n",
    "\n",
    "Why is there a big difference in circuit characteristics "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### The full solution for your reference\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T14:26:50.023422Z",
     "iopub.status.busy": "2024-05-07T14:26:50.023021Z",
     "iopub.status.idle": "2024-05-07T14:26:52.362431Z",
     "shell.execute_reply": "2024-05-07T14:26:52.361693Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Opening: https://platform.classiq.io/circuit/b4e9649f-2578-4dbb-bc4c-de943266395a?version=0.41.0.dev39%2B79c8fd0855\n"
     ]
    }
   ],
   "source": [
    "bound = 0.3\n",
    "\n",
    "\n",
    "@qfunc\n",
    "def main(target: Output[QArray]):\n",
    "    prepare_amplitudes(amplitudes=amplitudes, out=target, bound=bound)\n",
    "\n",
    "\n",
    "qprog = synthesize(create_model(main))\n",
    "show(qprog)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
